package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.DeviceDao;
import com.zretc.device.dao.UserDao;
import com.zretc.device.entity.Device;
import com.zretc.device.entity.Device_Need;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

public class MenuFrameUser extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField txtInput;
	private JTextField txtName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MenuFrameUser frame = new MenuFrameUser();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MenuFrameUser() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 807, 628);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("需求管理");
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("申请设备");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_needAddDialog dad = new Device_needAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示
				dad.setVisible(true);

			}
		});
		mnNewMenu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("查看申请");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
//				Device_needFindUserDialog dfud = new Device_needFindUserDialog(devNeed);
//				// 设置为模态属性
//				dfud.setModal(true);
//				// 显示
//				dfud.setVisible(true);
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("查询订单");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_needFindUserDialog dfud = new Device_needFindUserDialog();
				// 设置为模态属性
				dfud.setModal(true);
				// 显示
				dfud.setVisible(true);
			}
		});
		mnNewMenu.add(mntmNewMenuItem_4);

		JMenu mnNewMenu_1 = new JMenu("个人中心");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("修改密码");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_2);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("退出登录");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_3);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(32, 195, 675, 286);
		contentPane.add(scrollPane);
		// 建表
		table = new JTable();
		table.setRowHeight(25);
		fillTable(scrollPane);

		JLabel label = new JLabel("请输入查询信息");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(32, 133, 181, 38);
		contentPane.add(label);

		JLabel label_1 = new JLabel("请选择查询方式");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(32, 73, 181, 38);
		contentPane.add(label_1);

		JComboBox txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "设备编号", "设备名称" }));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(227, 79, 204, 30);
		contentPane.add(txtCB);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setColumns(10);
		txtInput.setBounds(227, 136, 204, 31);
		contentPane.add(txtInput);

		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String opString = txtCB.getSelectedItem() + "";
				// 取值
				String code = txtInput.getText();
				// 对复选框的内容进行判断
				if (opString.equals("设备编号")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						Device dev = fillTableByCo(scrollPane, code);
						if (dev == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该编号不存在，请重新输入");
						}
					}
				} else if (opString.equals("设备名称")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						List<Device> list = fillTableByName(scrollPane, code);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该名称不存在，请重新输入");
						}
					}
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(463, 133, 113, 31);
		contentPane.add(button);

		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fillTable(scrollPane);
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 20));
		button_1.setBounds(594, 133, 113, 31);
		contentPane.add(button_1);
		
		JLabel label_2 = new JLabel("库存查询");
		label_2.setFont(new Font("宋体", Font.PLAIN, 40));
		label_2.setBounds(282, 13, 182, 47);
		contentPane.add(label_2);
		
		JLabel lblNewLabel = new JLabel("欢迎您：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setBounds(562, 13, 80, 30);
		contentPane.add(lblNewLabel);
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 15));
		txtName.setBounds(634, 13, 73, 25);
		contentPane.add(txtName);
		txtName.setColumns(10);

		// 居中
		setLocationRelativeTo(null);
	}
	
	/**
	 * 带参构造
	 */
	public MenuFrameUser(String username) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 807, 628);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("需求管理");
		menuBar.add(mnNewMenu);

		JMenuItem mntmNewMenuItem = new JMenuItem("申请设备");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Device_needAddDialog dad = new Device_needAddDialog();
				// 设置为模态属性
				dad.setModal(true);
				// 显示
				dad.setVisible(true);

			}
		});
		mnNewMenu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("查看申请");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_needFindUserDialog1 dfud = new Device_needFindUserDialog1(username);
				// 设置为模态属性
				dfud.setModal(true);
				// 显示
				dfud.setVisible(true);
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("查询订单");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 跳转
				Device_needFindUserDialog dfud = new Device_needFindUserDialog();
				// 设置为模态属性
				dfud.setModal(true);
				// 显示
				dfud.setVisible(true);
			}
		});
		mnNewMenu.add(mntmNewMenuItem_4);

		JMenu mnNewMenu_1 = new JMenu("个人中心");
		menuBar.add(mnNewMenu_1);

		JMenuItem mntmNewMenuItem_2 = new JMenuItem("修改密码");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ModPassUser modpass = new ModPassUser();
				//设置为模态属性
				modpass.setModal(true);
				modpass.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_2);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("退出登录");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				UserLoginFrame user = new UserLoginFrame();
				user.setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_3);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(35, 195, 675, 286);
		contentPane.add(scrollPane);
		// 建表
		table = new JTable();
		table.setRowHeight(25);
		fillTable(scrollPane);

		JLabel label = new JLabel("请输入查询信息");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(32, 133, 181, 38);
		contentPane.add(label);

		JLabel label_1 = new JLabel("请选择查询方式");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(32, 73, 181, 38);
		contentPane.add(label_1);

		JComboBox txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "设备编号", "设备名称" }));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(227, 79, 204, 30);
		contentPane.add(txtCB);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setColumns(10);
		txtInput.setBounds(227, 136, 204, 31);
		contentPane.add(txtInput);

		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String opString = txtCB.getSelectedItem() + "";
				// 取值
				String code = txtInput.getText();
				// 对复选框的内容进行判断
				if (opString.equals("设备编号")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						Device dev = fillTableByCo(scrollPane, code);
						if (dev == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该编号不存在，请重新输入");
						}
					}
				} else if (opString.equals("设备名称")) {
					if (code.equals("")) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "查询内容不能为空");
					} else {
						// 调用方法
						List<Device> list = fillTableByName(scrollPane, code);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该名称不存在，请重新输入");
						}
					}
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(463, 133, 113, 31);
		contentPane.add(button);

		JButton button_1 = new JButton("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInput.setText("");
				fillTable(scrollPane);
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 20));
		button_1.setBounds(594, 133, 113, 31);
		contentPane.add(button_1);
		
		JLabel label_2 = new JLabel("库存查询");
		label_2.setFont(new Font("宋体", Font.PLAIN, 40));
		label_2.setBounds(282, 13, 182, 47);
		contentPane.add(label_2);
		
		JLabel lblNewLabel = new JLabel("欢迎您：");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		lblNewLabel.setBounds(562, 13, 80, 30);
		contentPane.add(lblNewLabel);
		
		txtName = new JTextField();
		UserDao user = new UserDao();
		User u=user.findUserById(username);
		txtName.setText(u.getUsercode());
		//设置为不可编辑
		txtName.setEditable(false);
		txtName.setFont(new Font("宋体", Font.PLAIN, 17));
		txtName.setBounds(634, 15, 73, 25);
		contentPane.add(txtName);
		txtName.setColumns(10);

		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * 查询所有
	 * 
	 * @param scrollPane
	 */
	private void fillTable(JScrollPane scrollPane) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceAll();
		// 获取list长度
		int row = list.size();
		// 创建二维数组
		Object[][] obj = new Object[row][6];
		// 迭代遍历
		for (int i = 0; i < obj.length; i++) {
			// 取值
			Device device = list.get(i);
			// 赋值
			obj[i][0] = device.getDevice_code();
			obj[i][1] = device.getDevice_name();
			obj[i][2] = device.getNow_num();
			obj[i][3] = device.getHigh_num();
			obj[i][4] = device.getLow_num();
			obj[i][5] = device.getTotal_num();
		}
		table.setModel(new DefaultTableModel(obj, new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0",
				"\u73B0\u6709\u5E93\u5B58", "\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u603B\u6570" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 通过编号查询，并输出在表格里
	 * 
	 * @param scrollPane
	 */
	private Device fillTableByCo(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找de方法
		Device device = dd.findDevByCo(code);
		//
		if (device != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][6];
			// 赋值
			obj[0][0] = device.getDevice_code();
			obj[0][1] = device.getDevice_name();
			obj[0][2] = device.getNow_num();
			obj[0][3] = device.getHigh_num();
			obj[0][4] = device.getLow_num();
			obj[0][5] = device.getTotal_num();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return device;
	}

	/**
	 * 根据名称查询
	 * 
	 * @param scrollPane
	 */
	private List<Device> fillTableByName(JScrollPane scrollPane, String code) {
		// 创建对象
		DeviceDao dd = new DeviceDao();
		// 调用查找所有的方法
		List<Device> list = dd.findDeviceByName(code);
		if (list != null) {
			// 获取list长度
			int row = list.size();
			// 创建二维数组
			Object[][] obj = new Object[row][6];
			// 迭代遍历
			for (int i = 0; i < obj.length; i++) {
				// 取值
				Device device = list.get(i);
				// 赋值
				obj[i][0] = device.getDevice_code();
				obj[i][1] = device.getDevice_name();
				obj[i][2] = device.getNow_num();
				obj[i][3] = device.getHigh_num();
				obj[i][4] = device.getLow_num();
				obj[i][5] = device.getTotal_num();
			}

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u8BBE\u5907\u7F16\u53F7", "\u8BBE\u5907\u540D\u79F0", "\u73B0\u6709\u5E93\u5B58",
							"\u6700\u5927\u5E93\u5B58", "\u6700\u5C0F\u5E93\u5B58", "\u5E93\u5B58\u603B\u6570" }));
			scrollPane.setViewportView(table);
		}
		return list;
	}
}
